package com.version1.diabeco.ui.comida;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.version1.diabeco.R;
import com.version1.diabeco.data.comida.AlimentoDaoImpl;
import com.version1.diabeco.data.comida.ComidaDaoImpl;
import com.version1.diabeco.data.glucosa.GlucosaDaoImpl;
import com.version1.diabeco.model.comida.Comida;
import com.version1.diabeco.model.glucosa.Glucosa;
import com.version1.diabeco.ui.glucosa.ActivityListaRegistrosGlucosa;

public class ActivityListaRegistrosComidas extends Activity {

	private static final int REQUEST_CODE = 1236;
	public static final String INTENT_VALUE_COMIDA = "Comida";

	private List<Comida> comidas;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_lista_registros_comida);

		this.cargarPantalla();
	}

	public void agregarComidaAction(View view) {

		Intent intent = new Intent(this, ActivityRegistroComida.class);
		Comida comida = new Comida();
		intent.putExtra(INTENT_VALUE_COMIDA, comida);
		startActivityForResult(intent, REQUEST_CODE);
	}

	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

//		List<Comida> lista = ComidaDaoImpl.getInstance(this).selectAll();
		List<Comida> lista = ComidaDaoImpl.getInstance(this).select5Dias();

		this.setComidas(lista);

		this.cargarPantalla();

	}

	private void cargarPantalla() {

//		List<Comida> lista = ComidaDaoImpl.getInstance(this).selectAll();
		List<Comida> lista = ComidaDaoImpl.getInstance(this).select5Dias();

		this.setComidas(lista);

		List<Comida> comidas = this.getComidas();

		if (!comidas.isEmpty()) {
			ListView measuresList = (ListView) findViewById(R.id.list_registros_comida);

			ComidaAdapter adapter = new ComidaAdapter(this, comidas);
			measuresList.setAdapter(adapter);

			measuresList.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

					ArrayAdapter<Comida> arrayAdapter = (ArrayAdapter<Comida>) ((ListView) parent)
							.getAdapter();
					Comida comida = arrayAdapter.getItem(position);

					Intent intent = new Intent(parent.getContext(), ActivityRegistroComida.class);
					intent.putExtra(INTENT_VALUE_COMIDA, comida);
					startActivityForResult(intent, REQUEST_CODE);				}
				
			});

			measuresList.setOnItemLongClickListener(new OnItemLongClickListener() {

				public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

					// Mostramos el menu contextual y ejecutamos la accion
					dialogMenuContextual(R.string.menu_contextual, R.array.lb_menu_contextual, position);
					
//					ArrayAdapter<Comida> arrayAdapter = (ArrayAdapter<Comida>) ((ListView) parent)
//							.getAdapter();
//					Comida comida = arrayAdapter.getItem(position);
//
//					ComidaDaoImpl.getInstance(ActivityListaRegistrosComidas.this).deleteById(
//							comida.getId());
//					getComidas().remove(comida);
//
//					arrayAdapter.notifyDataSetChanged();

					return true;
				}
			});
		} else {
			// TODO inform the user there are no comidas taken
		}
	}

	public List<Comida> getComidas() {
		return comidas;
	}

	public void setComidas(List<Comida> comidas) {
		this.comidas = comidas;
	}

	
	/** PopUp para Mostrar una lista de valores **/
	public void dialogMenuContextual(int title, int list, final int pos) {

		AlertDialog popup;
		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		builder.setCancelable(true);
		builder.setTitle(title);
		builder.setIcon(android.R.drawable.ic_menu_more);
		builder.setItems(list, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int selectedItem) {

				mostrarMensajeConfirmacion(pos);
				dialog.cancel();
			}
		});

		// Cremos el PopUp y lo mostramos
		popup = builder.create();
		popup.show();
	}

	/** PopUp para Mostrar una lista de valores **/
	public void mostrarMensajeConfirmacion(final int pos) {
		
	AlertDialog popup;
	AlertDialog.Builder builder = new AlertDialog.Builder(this);
	 
    builder.setTitle(R.string.titulo_confimacion);
    builder.setMessage(R.string.pregunta_confirmacion);
    builder.setIcon(android.R.drawable.ic_dialog_alert);
    
    	//Boton Aceptar
    	builder.setPositiveButton(R.string.btn_aceptar, new DialogInterface.OnClickListener() {

    		public void onClick(DialogInterface dialog, int position) {

    			ListView listView = (ListView) findViewById(R.id.list_registros_comida);
    			
    			Comida comida = (Comida) (listView.getItemAtPosition(pos));            	
    			ArrayAdapter<Glucosa> arrayAdapter = (ArrayAdapter<Glucosa>) listView.getAdapter();    	

    			ComidaDaoImpl.getInstance(ActivityListaRegistrosComidas.this).deleteById(comida.getId());
    			getComidas().remove(comida);

    			arrayAdapter.notifyDataSetChanged();     			
    			
    			Toast.makeText(ActivityListaRegistrosComidas.this, "Comida Borrada", Toast.LENGTH_SHORT).show();

    	        dialog.cancel();
    	    	}
        	});

    	//Boton Cancelar    
	    builder.setNegativeButton(R.string.btn_cancelar, new DialogInterface.OnClickListener() {
	    public void onClick(DialogInterface dialog, int which) {
		        dialog.cancel();
	    }
	    });
 
    //Cremos el PopUp y lo mostramos
	popup = builder.create();
	popup.show();		
	}
	
}
